package com.dc.project.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dc.base.common.po.BasePO;
import com.dc.base.po.msg.MsgInside;
import com.dc.common.enumeration.ErrorCodeEnum;
import com.dc.common.util.BeanUtil;
import com.dc.common.util.ExcelReadValidationUtil;
import com.dc.common.util.ExcelUtil;
import com.dc.common.vo.BusinessException;
import com.dc.project.demo.dto.PmAttributeFormDTO;
import com.dc.project.demo.mapper.PmAttributeMapper;
import com.dc.project.demo.po.PmAttribute;
import com.dc.project.demo.query.PmAttributeQuery;
import com.dc.project.demo.service.PmAttributeService;
import com.dc.project.demo.vo.PmAttributeVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 属性管理接口
 *
 * @author Enzo
 * @date 2020-7-8
 */
@Service
public class PmAttributeServiceImpl implements PmAttributeService {
    @Resource
    private PmAttributeMapper pmAttributeMapper;


    @Override
    public PmAttribute insertData(PmAttributeFormDTO dto) {
        PmAttribute po = checkData(dto);
        pmAttributeMapper.insert(po);
        return po;
    }

    private PmAttribute checkData(PmAttributeFormDTO dto) {
        if (StringUtils.isEmpty(dto.getAttrName())) {
            throw new BusinessException("属性不允许为空");
        }
        PmAttribute po = new PmAttribute();
        BeanUtil.copy(dto, po);
        return po;
    }

    /**
     * 修改属性
     *
     * @param dto
     * @author Enzo
     * @date 2020-7-8
     **/
    @Override
    public PmAttribute updateData(PmAttributeFormDTO dto) {
        if (StringUtils.isEmpty(dto.getId())) {
            throw new BusinessException("主键id不允许为空");
        }
        if (dto.getVersion() == null) {
            throw new BusinessException(ErrorCodeEnum.UPDATE_VES_NOT);
        }
        PmAttribute po = checkData(dto);
        pmAttributeMapper.updateById(po);
        return po;
    }

    @Override
    public int deleteData(String pk) {
        List<String> ids = new ArrayList<>();
        ids.add(pk);
        int res = deleteBatch(ids);
        return res;
    }

    @Override
    public int deleteBatch(List<String> ids) {
        if (ids == null || ids.size() == 0) {
            throw new BusinessException("请选择删除的记录");
        }
        int res = pmAttributeMapper.deleteBatchIds(ids);
        return res;
    }

    private MPJLambdaWrapper<PmAttribute> getQuery(PmAttributeQuery query) {
        MPJLambdaWrapper<PmAttribute> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.orderByDesc(BasePO::getGmtCreate);
        PmAttribute po = new PmAttribute();
        BeanUtil.copy(query, po);
        queryWrapper.setEntity(po);
        return queryWrapper;
    }

    @Override
    public PmAttribute getById(String pk) {
        PmAttribute attribute = pmAttributeMapper.selectById(pk);
        if (attribute == null) {
            throw new BusinessException("未查询到数据");
        }
        return attribute;
    }

    @Override
    public PageInfo<PmAttribute> pageData(PmAttributeQuery query) {
        MPJLambdaWrapper<PmAttribute> queryWrapper = getQuery(query);
        PageHelper.startPage(query.getPageNum(), query.getPageSize());
        List<PmAttribute> list = pmAttributeMapper.selectList(queryWrapper);
        return PageInfo.of(list);
    }

    @Override
    public List<PmAttributeFormDTO> importData(MultipartFile excelFile) {
        if (excelFile == null || excelFile.getSize() < 1) {
            throw new BusinessException("请至少选择一条导入的记录信息");
        }
        ExcelReadValidationUtil<PmAttributeFormDTO> excelUtil = new ExcelReadValidationUtil<PmAttributeFormDTO>() {
        };
        List<PmAttributeFormDTO> dtoList = excelUtil.read(excelFile, (rowList, errorMsg) -> {
            for (int i = 0; i < rowList.size(); i++) {
                PmAttributeFormDTO dto = rowList.get(i);
                //errorMsg.addError(i, "name", "xxx不能为空");
            }
        }, false);
        for (PmAttributeFormDTO dto : dtoList) {
            insertData(dto);
        }
        return dtoList;
    }

    @Override
    public String exportData(PmAttributeQuery query) {
        MPJLambdaWrapper<PmAttribute> queryWrapper = getQuery(query);
        List<PmAttribute> list = pmAttributeMapper.selectList(queryWrapper);
        return ExcelUtil.exportTemplateComment(list, "PmAttribute.xlsx", null);
    }

    @Override
    public PageInfo<PmAttributeVO> pageMyMsg(PmAttributeQuery query) {
        MPJLambdaWrapper<PmAttribute> queryWrapper = getQuery(query);
        queryWrapper.selectAll(PmAttribute.class)
                .innerJoin(MsgInside.class, MsgInside::getOriginId, PmAttribute::getId)
                .eq(MsgInside::getReceiveUser, query.getReceiveUserId())
                .selectAs(MsgInside::getMsgStatus, PmAttributeVO::getMsgStatus)
                .selectAs(MsgInside::getProcessStatus, PmAttributeVO::getProcessUserStatus)
                .in(query.getMsgStatusList() != null && query.getMsgStatusList().length > 0, MsgInside::getMsgStatus, query.getMsgStatusList());
        PageHelper.startPage(query.getPageNum(), query.getPageSize());
        List<PmAttributeVO> list = pmAttributeMapper.selectMsgList(query);
        return PageInfo.of(list);
    }
}
